package com.rth.vedio.application.interceptor;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.rth.common.context.LoginContextHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

public class FeignRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        if (Objects.nonNull(request.getHeader("loginId"))){
            String loginId = LoginContextHolder.getLoginId();
            if (StringUtils.isNotBlank(loginId)){
                requestTemplate.header("loginId",loginId);
            }
        }
    }
}
